package com.shiku.imclient;

import com.shiku.imserver.common.SessionContext;
import com.shiku.imserver.common.message.AuthMessage;
import com.shiku.imserver.common.message.MessageHead;
import com.shiku.imserver.common.proto.MessageProBuf;
import com.shiku.imserver.common.tcp.TcpPacket;
import com.shiku.imserver.common.utils.ImUtils;
import com.shiku.imserver.common.utils.ProBufUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;

public abstract class BaseClientListener implements ClientAioListener {
    private static Logger log = LoggerFactory.getLogger(BaseClientListener.class);
    private BaseIMClient imClient;

    public abstract AuthMessage authUserMessage(ChannelContext channelContext, BaseIMClient baseIMClient);

    public void setImClient(BaseIMClient imClient2) {
        this.imClient = imClient2;
    }

    public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {
        log.info("onAfterConnected channelContext:{}, isConnected:{}, isReconnect:{}", new Object[]{channelContext, Boolean.valueOf(isConnected), Boolean.valueOf(isReconnect)});
        channelContext.setAttribute(new SessionContext());
        channelContext.userid = this.imClient.userId;
        Thread.sleep(10);
        AuthMessage authMessage = authUserMessage(channelContext, this.imClient);
        if (authMessage == null) {
            authMessage = authServerUser(channelContext);
        }
        Tio.bindUser(channelContext, ImUtils.parseBareUserId(authMessage.getMessageHead().getFrom()));
        Tio.send(channelContext, new TcpPacket((short)5, ProBufUtils.encodeMessageBody(authMessage, MessageProBuf.AuthMessage.getDescriptor())));
    }

    private AuthMessage authServerUser(ChannelContext channelContext) {
        MessageHead messageHead = new MessageHead();
        messageHead.setChatType((byte) 1);
        if (channelContext.userid == null) {
            channelContext.userid = this.imClient.userId;
        }
        messageHead.setFrom(this.imClient.connStr);
        AuthMessage authMessage = new AuthMessage();
        authMessage.setPassword("123456");
        authMessage.setToken("token");
        authMessage.setMessageHead(messageHead);
        return authMessage;
    }

    public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {
    }

    public void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) {
    }

    public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {
    }

    public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {
    }

    public void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {
    }
}
